{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from src import config, utils\n",
    "import logging\n",
    "from sklearn.model_selection import KFold\n",
    "import pickle\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "train = pd.read_csv(config.pj_root + 'data/a_val.csv', index_col='no')\n",
    "\n",
    "X = train.values[:, 1:]\n",
    "Y = train.values[:, 0:1].reshape((-1,))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# config.model_para_a['base_score'] = 0.51\n",
    "# config.model_para_a['max_delta_step'] = 0.1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:root:Use 4 Folds...\n",
      "INFO:root:{'max_depth': 5, 'n_estimators': 40, 'base_score': 0.5}\n",
      "INFO:root:Fold 1/4 Score: 0.743460 \n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "实际flag=1数量: 1318   预测概率大于0.5数量: 85\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:root:Fold 2/4 Score: 0.746648 \n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "实际flag=1数量: 1257   预测概率大于0.5数量: 64\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:root:Fold 3/4 Score: 0.734888 \n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "实际flag=1数量: 1278   预测概率大于0.5数量: 76\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:root:Fold 4/4 Score: 0.748773 \n",
      "INFO:root:Avg score 0.743442. \n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "实际flag=1数量: 1282   预测概率大于0.5数量: 80\n"
     ]
    }
   ],
   "source": [
    "logging.info('Use %d Folds...' % config.kfold_k)\n",
    "logging.info(config.model_para_a)\n",
    "kf = KFold(n_splits=config.kfold_k, shuffle=True)\n",
    "all_score = 0\n",
    "for i, (train_index, test_index) in enumerate(kf.split(X)):\n",
    "    X_train, X_test = X[train_index], X[test_index]\n",
    "    y_train, y_test = Y[train_index], Y[test_index]\n",
    "    model = config.model(**config.model_para_a)\n",
    "    model.fit(X_train, y_train)\n",
    "    y_pred = model.predict_proba(X_test)\n",
    "    score = utils.report(y_test, y_pred[:, 1], True)\n",
    "    all_score += score\n",
    "    logging.info('Fold %d/%d Score: %f ' % (i + 1, config.kfold_k, score))\n",
    "logging.info('Avg score %f. ' % (all_score / config.kfold_k))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:root:Start training....\n",
      "INFO:root:{'max_depth': 5, 'n_estimators': 40, 'base_score': 0.5}\n"
     ]
    }
   ],
   "source": [
    "#valall\n",
    "X_train = X\n",
    "y_train = Y\n",
    "logging.info('Start training....')\n",
    "logging.info(config.model_para_a)\n",
    "model = config.model(**config.model_para_a)\n",
    "model.fit(X_train, y_train)\n",
    "with open(config.pj_root + 'model/%s_for_a.mo' % config.model.__name__, 'wb') as f:\n",
    "    pickle.dump(model, f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "b_val = pd.read_csv(config.pj_root + 'data/val.csv', index_col='no')\n",
    "assert b_val.columns[0] == 'flag'\n",
    "a_feature = pd.DataFrame(index=b_val.index)\n",
    "pred = model.predict_proba(b_val.values[:,1:])\n",
    "pred = pred[:, 1]\n",
    "a_feature = a_feature.join(pd.DataFrame(pred, index=a_feature.index, columns=['a_feature']))\n",
    "a_feature.to_csv(config.pj_root + 'data/a_feature.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# i = pd.DataFrame({'feature':train.columns[1:], 'importance':model.feature_importances_})\n",
    "# i = i[i.importance>0].sort_values('importance', ascending=False)\n",
    "# i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "b_test = pd.read_csv(config.pj_root + 'data/test.csv', index_col='no')\n",
    "assert b_test.columns[0] != 'flag'\n",
    "a_feature = pd.DataFrame(index=b_test.index)\n",
    "pred = model.predict_proba(b_test.values[:, :])\n",
    "pred = pred[:, 1]\n",
    "a_feature = a_feature.join(pd.DataFrame(pred, index=a_feature.index, columns=['a_feature']))\n",
    "a_feature.to_csv(config.pj_root + 'data/a_feature_test.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "assert b_val.columns[1:].tolist() == b_test.columns[0:].tolist()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "assert train.columns[1:].tolist() == b_val.columns[1:].tolist() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [conda root]",
   "language": "python",
   "name": "conda-root-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
